package org.jivesoftware.smackx.bookmark;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smackx.packet.PrivateData;
import org.jivesoftware.smackx.provider.PrivateDataProvider;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class Bookmarks
  implements PrivateData
{
  private List<BookmarkedConference> bookmarkedConferences;
  private List<BookmarkedURL> bookmarkedURLS = new ArrayList();

  public Bookmarks()
  {
    this.bookmarkedConferences = new ArrayList();
  }

  private static BookmarkedConference getConferenceStorage(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    String str1 = paramXmlPullParser.getAttributeValue("", "name");
    String str2 = paramXmlPullParser.getAttributeValue("", "autojoin");
    BookmarkedConference localBookmarkedConference = new BookmarkedConference(paramXmlPullParser.getAttributeValue("", "jid"));
    localBookmarkedConference.setName(str1);
    localBookmarkedConference.setAutoJoin(Boolean.valueOf(str2).booleanValue());
    int i = 0;
    while (true)
    {
      int j;
      do
        while (true)
        {
          while (true)
          {
            while (true)
            {
              if (i != 0)
                return localBookmarkedConference;
              j = paramXmlPullParser.next();
              if ((j != 2) || (!("nick".equals(paramXmlPullParser.getName()))))
                break;
              localBookmarkedConference.setNickname(paramXmlPullParser.nextText());
            }
            if ((j != 2) || (!("password".equals(paramXmlPullParser.getName()))))
              break;
            localBookmarkedConference.setPassword(paramXmlPullParser.nextText());
          }
          if ((j != 2) || (!("shared_bookmark".equals(paramXmlPullParser.getName()))))
            break;
          localBookmarkedConference.setShared(true);
        }
      while ((j != 3) || (!("conference".equals(paramXmlPullParser.getName()))));
      i = 1;
    }
  }

  private static BookmarkedURL getURLStorage(XmlPullParser paramXmlPullParser)
    throws IOException, XmlPullParserException
  {
    boolean bool;
    BookmarkedURL localBookmarkedURL;
    int i;
    String str1 = paramXmlPullParser.getAttributeValue("", "name");
    String str2 = paramXmlPullParser.getAttributeValue("", "url");
    String str3 = paramXmlPullParser.getAttributeValue("", "rss");
    if ((str3 != null) && ("true".equals(str3)))
    {
      bool = true;
      localBookmarkedURL = new BookmarkedURL(str2, str1, bool);
      i = 0;
    }
    while (true)
    {
      int j;
      do
        while (true)
        {
          while (i != 0)
          {
            return localBookmarkedURL;
            bool = false;
          }
          j = paramXmlPullParser.next();
          if ((j != 2) || (!("shared_bookmark".equals(paramXmlPullParser.getName()))))
            break;
          localBookmarkedURL.setShared(true);
        }
      while ((j != 3) || (!("url".equals(paramXmlPullParser.getName()))));
      i = 1;
    }
  }

  public void addBookmarkedConference(BookmarkedConference paramBookmarkedConference)
  {
    this.bookmarkedConferences.add(paramBookmarkedConference);
  }

  public void addBookmarkedURL(BookmarkedURL paramBookmarkedURL)
  {
    this.bookmarkedURLS.add(paramBookmarkedURL);
  }

  public void clearBookmarkedConferences()
  {
    this.bookmarkedConferences.clear();
  }

  public void clearBookmarkedURLS()
  {
    this.bookmarkedURLS.clear();
  }

  public List<BookmarkedConference> getBookmarkedConferences()
  {
    return this.bookmarkedConferences;
  }

  public List<BookmarkedURL> getBookmarkedURLS()
  {
    return this.bookmarkedURLS;
  }

  public String getElementName()
  {
    return "storage";
  }

  public String getNamespace()
  {
    return "storage:bookmarks";
  }

  public void removeBookmarkedConference(BookmarkedConference paramBookmarkedConference)
  {
    this.bookmarkedConferences.remove(paramBookmarkedConference);
  }

  public void removeBookmarkedURL(BookmarkedURL paramBookmarkedURL)
  {
    this.bookmarkedURLS.remove(paramBookmarkedURL);
  }

  public String toXML()
  {
    Iterator localIterator2;
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<storage xmlns=\"storage:bookmarks\">");
    Iterator localIterator1 = getBookmarkedURLS().iterator();
    if (!(localIterator1.hasNext()))
      localIterator2 = getBookmarkedConferences().iterator();
    while (true)
    {
      label153: BookmarkedConference localBookmarkedConference;
      do
      {
        while (true)
        {
          BookmarkedURL localBookmarkedURL;
          do
          {
            if (localIterator2.hasNext())
              break label153;
            localStringBuilder.append("</storage>");
            return localStringBuilder.toString();
            localBookmarkedURL = (BookmarkedURL)localIterator1.next();
          }
          while (localBookmarkedURL.isShared());
          localStringBuilder.append("<url name=\"").append(localBookmarkedURL.getName()).append("\" url=\"").append(localBookmarkedURL.getURL()).append("\"");
          if (localBookmarkedURL.isRss())
            localStringBuilder.append(" rss=\"").append(true).append("\"");
          localStringBuilder.append(" />");
        }
        localBookmarkedConference = (BookmarkedConference)localIterator2.next();
      }
      while (localBookmarkedConference.isShared());
      localStringBuilder.append("<conference ");
      localStringBuilder.append("name=\"").append(localBookmarkedConference.getName()).append("\" ");
      localStringBuilder.append("autojoin=\"").append(localBookmarkedConference.isAutoJoin()).append("\" ");
      localStringBuilder.append("jid=\"").append(localBookmarkedConference.getJid()).append("\" ");
      localStringBuilder.append(">");
      if (localBookmarkedConference.getNickname() != null)
        localStringBuilder.append("<nick>").append(localBookmarkedConference.getNickname()).append("</nick>");
      if (localBookmarkedConference.getPassword() != null)
        localStringBuilder.append("<password>").append(localBookmarkedConference.getPassword()).append("</password>");
      localStringBuilder.append("</conference>");
    }
  }

  public static class Provider
  implements PrivateDataProvider
  {
    public PrivateData parsePrivateData(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      Bookmarks localBookmarks = new Bookmarks();
      int i = 0;
      while (true)
      {
        int j;
        do
          while (true)
          {
            while (true)
            {
              BookmarkedURL localBookmarkedURL;
              do
              {
                if (i != 0)
                  return localBookmarks;
                j = paramXmlPullParser.next();
                if ((j != 2) || (!("url".equals(paramXmlPullParser.getName()))))
                  break label64;
                localBookmarkedURL = Bookmarks.access$0(paramXmlPullParser);
              }
              while (localBookmarkedURL == null);
              localBookmarks.addBookmarkedURL(localBookmarkedURL);
            }
            label64: if ((j != 2) || (!("conference".equals(paramXmlPullParser.getName()))))
              break;
            localBookmarks.addBookmarkedConference(Bookmarks.access$1(paramXmlPullParser));
          }
        while ((j != 3) || (!("storage".equals(paramXmlPullParser.getName()))));
        i = 1;
      }
    }
  }
}